﻿using DevExpress.XtraEditors;
using Model;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Tools;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.报表窗体
{
    public partial class 产量报表 : Form
    {
        public 产量报表()
        {
            InitializeComponent();
            uiDatePicker2.Value = DateTime.Now;
            uiDatePicker1.Value = uiDatePicker2.Value.AddDays(-QueryTime.间隔);
            cmbjigong.DataSource = YuanGongTableService .GetYuanGongTablelst ().Select (x=>x.Xingming ).ToList();
            cmbjigong.Text = "";
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            query();
        }
        private string QueryString()
        {
            return $"select *,(select top(1) liuzhuancard.Shuliang from liuzhuancard  where liuzhuancard.CardNum =liuzhuanlog.CardNum) as Num from liuzhuanlog where Employee like '%{cmbjigong.Text}%'  and " +
                $"_date >='{uiDatePicker1.Value.Date}' and _date<='{uiDatePicker2.Value.Date.AddDays(1)}'  order by _date desc";
        }
        private void query()
        {
            //this.ShowWaitForm();
            try
            {
                var s = QueryString();
                var logs = Connect.dbcontext.Ado.SqlQuery<liuzhuanlog>(s);
                var totalstring=$"select sum(Num) as Num,Employee from  liuzhuanlog where _date >='{uiDatePicker1.Value.Date}' and _date<='{uiDatePicker2.Value.Date.AddDays(1)}' Group by Employee order by Employee ";
                gridControl1.DataSource=Connect.dbcontext.Ado.GetDataTable(totalstring);
                label4.Text =Connect.dbcontext.Queryable<liuzhuanlog>().Where(x => x._date >= uiDatePicker1.Value.Date && x._date <= uiDatePicker2.Value.Date.AddDays(1)
                &&x.GongYi=="上卷完毕").Sum(x=>x.Num).ToString();
                label5.Text = Connect.dbcontext.Queryable<liuzhuanlog>().Where(x => x._date >= uiDatePicker1.Value.Date && x._date <= uiDatePicker2.Value.Date.AddDays(1)
                && x.GongYi == "翻卷").Sum(x => x.Num).ToString();
                label6.Text = Connect.dbcontext.Queryable<liuzhuanlog>().Where(x => x._date >= uiDatePicker1.Value.Date && x._date <= uiDatePicker2.Value.Date.AddDays(1)
&& x.GongYi == "下机").Sum(x => x.Num).ToString();
                chart1.Series.Clear();
                var jingchangdt = Connect.dbcontext.Ado.GetDataTable($"select sum(Num) as Num,Employee from  liuzhuanlog where _date >='{uiDatePicker1.Value.Date}' and _date<='{uiDatePicker2.Value.Date.AddDays(1)}'" +
                    $" and Employee in (select Xingming from YuanGongTable where Department='检验' ) Group by Employee order by Employee ");
                var series = new Series("退卷产量");
                series.ChartType = SeriesChartType.Column;
                // 设置悬停提示格式
                series.ToolTip = "日期: #VALX\n数量: #VALY";  // 使用内置变量占位符
                                                          //series.IsValueShownAsLabel = false;  // 关闭默认标签显示
                                                          // 绑定数据
                foreach (DataRow row in jingchangdt.Rows)
                {
                    series.Points.AddXY(
                        row["Employee"].ToString(),
                        row["Num"]);
                }
                chart1.Series.Add(series);
                // 设置chart2
                chart2.Series.Clear();
                 jingchangdt = Connect.dbcontext.Ado.GetDataTable($"select sum(Num) as Num,Employee from  liuzhuanlog where _date >='{uiDatePicker1.Value.Date}' and _date<='{uiDatePicker2.Value.Date.AddDays(1)}'" +
                    $" and Employee in (select Xingming from YuanGongTable where Department='生产' ) and Gongyi='下机' Group by Employee order by Employee ");
                var series1 = new Series("生产产量");
                series1.ChartType = SeriesChartType.Column;
                // 设置悬停提示格式
                series1.ToolTip = "日期: #VALX\n数量: #VALY";  // 使用内置变量占位符
             
                                                          // 绑定数据
                foreach (DataRow row in jingchangdt.Rows)
                {
                    series1.Points.AddXY(
                       row["Employee"].ToString(),
                        row["Num"]);
                }
                chart2.Series.Add(series1);
                // 根据数据范围自动计算刻度间隔
                chart2.ChartAreas[0].AxisY.IsMarginVisible = true;
                chart2.ChartAreas[0].RecalculateAxesScale();
                chart1.ChartAreas[0].AxisY.IsMarginVisible = true;
                chart1.ChartAreas[0].RecalculateAxesScale();
                gridControl2.DataSource = logs;
               
            }
            catch (Exception ex) 
            {
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
            //this.HideWaitForm();
        }

        private void 产量报表_Load(object sender, EventArgs e)
        {
            query();
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl2, "生产统计报表");
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl2, "生产统计报表");
        }

        private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.FocusedRowHandle >= 0)
                {
                    var log = gridView1.GetFocusedRow() as liuzhuanlog;
                    if (log != null)
                    {
                        gridView2.DataSource = null;
                        gridView2.DataSource = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao ==log.Beizhu);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void gridView3_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView3.FocusedRowHandle >= 0)
                {
                    var log = gridView3.GetFocusedDataRow() as DataRow;
                    if (log != null)
                    {
                     gridControl3.DataSource=  Connect.dbcontext.Queryable<liuzhuanlog>().LeftJoin<danjumingxitable>((x, y) => x.Beizhu == y.danhao).LeftJoin<Liuzhuancard>((x, y, z) => x.CardNum == z.CardNum).Where(x => x._date >= uiDatePicker1.Value.Date
                      && x._date <= uiDatePicker2.Value.Date.AddDays(1) && x.Employee == log["Employee"].ToString()&&x.GongYi!="上机").OrderByDescending(x => x._date)
                     .Select((x, y, z) => new LiuzhuanResult()
                     {
                         CardNum = x.CardNum,
                         CustomName = z.Customer,
                         MachineID = x.MachineID,
                         _date = x._date,
                         Employee = x.Employee,
                         GongyiYaoqiu = x.GongyiYaoqiu,
                         GongYi = x.GongYi,
                         Num = y.UpRolledNum,
                         SingleClothLength = y.UpRolledNum,
                         Rolls = y.UpRolledRolls,
                         Pingming = y.pingming,
                         JiaZhiHao = x.JiaZhiHao,
                         houzhengli = z.Beizhu,
                         Beizhu = x.Beizhu,
                         LotNum = y.ganghao
                     }).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowErrorBox(ex.Message);
            }
          
        }
    }
}
